#!/usr/bin/python

import os
import sys
import re
import mongodbConfig as mongo
import ceilometerConfig as ceilometer

class ceilomter():
    global Excute
    Excute = os.system

    def __init__(self):
	pass

    def install(self,ip,token,monit,dev):
        mongo.mount(monit,dev)
        mongo.install(monit)
	IPs = mongo.getLocalIp(monit)
	print IPs
        mongo.config(IPs,monit)
        mongo.run(monit)
        mongo.create(ip,monit)
#        Excute("source admin-openrc.sh")
        Excute("ssh "+monit+" \"openstack --os-url http://"+ip+":35357/v2.0 --os-token "+token.split('\n')[0]+" user create --password 123456a? ceilometer\"")
        Excute("ssh "+monit+" \"openstack --os-url http://"+ip+":35357/v2.0 --os-token "+token.split('\n')[0]+" role add --project service --user ceilometer admin\"")
        Excute("ssh "+monit+" \"openstack --os-url http://"+ip+":35357/v2.0 --os-token "+token.split('\n')[0]+" service create --name ceilometer --description \"Telemetry\" metering\"")
        Excute('ssh '+monit+' \'openstack --os-url http://'+ip+':35357/v2.0 --os-token '+token.split('\n')[0]+' endpoint create --publicurl http://'+ip+":8777 --internalurl http://"+ip+":8777 --adminurl http://"+ip+":8777 --region RegionOne metering\'")
        Excute('ssh '+monit+' \'yum -y install openstack-ceilometer-api openstack-ceilometer-collector openstack-ceilometer-notification openstack-ceilometer-central openstack-ceilometer-alarm python-ceilometerclient\'')
        return


    def config(self,ip,token,monit):
        ceilometer.config(ip,token,monit)
        return


    def run(self,monit):
        Excute("ssh "+monit+" \"systemctl enable openstack-ceilometer-api.service openstack-ceilometer-collector.service openstack-ceilometer-notification.service openstack-ceilometer-alarm-evaluator.service openstack-ceilometer-alarm-notifier.service\"")
        Excute("ssh "+monit+" \"systemctl restart openstack-ceilometer-api.service openstack-ceilometer-collector.service openstack-ceilometer-collector.service openstack-ceilometer-notification.service openstack-ceilometer-alarm-evaluator.service openstack-ceilometer-alarm-notifier.service\"")

    def main(self,ip,token,monit,dev):
        self.install(ip,token,monit,dev)
	self.config(ip,token,monit)
	self.run(monit)
